package com.konsung.ksdc.common.widget;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.konsung.ksdc.R;
import com.konsung.ksdc.common.utils.DensityUtils;

/**
 * 通用消息提示框
 */
public class ConfirmPopupWindow extends PopupWindow {
    private View view;
    TextView tvTitle;
    protected TextView tvTips;
    Button btnCancel;
    Button btnConfirm;
    private Context context;
    private final LinearLayout llPop;

    /**
     * 构造方法
     * @param context 上下文
     * @param confirm 确认
     * @param cancel 取消
     */
    public ConfirmPopupWindow(Context context, View.OnClickListener confirm,
            View.OnClickListener cancel) {

        super(context);
        this.context = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context
                .LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.confirm_popup, null);

        llPop = (LinearLayout) view.findViewById(R.id.ll_pop);
        tvTitle = (TextView) view.findViewById(R.id.tv_title);
        tvTips = (TextView) view.findViewById(R.id.tv_tips);
        btnCancel = (Button) view.findViewById(R.id.btn_cancel);
        btnConfirm = (Button) view.findViewById(R.id.btn_confirm);

        btnConfirm.setOnClickListener(confirm);
        btnCancel.setOnClickListener(cancel);

        //设置PopupWindow的View
        this.setContentView(view);
        //设置PopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置PopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置PopupWindow弹出窗体可点击
        this.setFocusable(false);
        this.getBackground().setAlpha(100);
    }

    /**
     * 设置弹窗标题
     * @param title 标题
     */
    public void setTitle(String title) {
        tvTitle.setText(title);
    }

    /**
     * 弹窗提示内容
     * @param tips 提示内容
     */
    public void setTips(String tips) {
        tvTips.setText(tips);
    }

    /**
     * 设置内容的字体大小和位置
     * @param size 大小
     * @param width 距底部间距
     */
    public void setTipsLayout(int size, int width) {
        tvTips.setTextSize(size);
        llPop.setMinimumHeight(DensityUtils.dp2px(context, width));
        tvTips.setGravity(Gravity.CENTER);
    }
}
